import type { TableProps } from "@layui/layui-vue/types/component/table/index.vue";
import type { SchemaProps } from "@layui/json-schema-form/types/component/form/types";

import { ExtractPropTypes, PropType } from "vue";
import type DynamicTable from "./dynamic-table.vue";
import {
  EditableType,
  LoadDataParams,
  OnCancel,
  OnChangeCallbackParams,
  OnSave,
} from "./types/table";
import { isBoolean } from "../../../utils/is";
import { TableColumn } from "./types";
interface renderCallback {
  beforeBinded: (tableData: any, tableInstance: any) => void;
  onBinded: (tableData: any, tableInstance: any) => void;
}
export const dynamicTableProps = {
  rowKey: {
    type: [String, Function] as PropType<string | GetRowKey<any>>,
    default: "id",
  },
  /** 是否显示索引号 */
  enableIndexRow: {
    type: Boolean as PropType<boolean>,
    default: false,
  },
  /**是否显示多选框*/
  enableCheckBox: {
    type: Boolean as PropType<boolean>,
    default: false,
  },
  /**是否显示单选框*/
  enableRadio: {
    type: Boolean as PropType<boolean>,
    default: false,
  },
  /** 是否启用二级表头*/
  enableSubHeader: {
    type: Boolean as PropType<boolean>,
    default: false,
  },
  /** 是否启用自定义列*/
  enableCustomColumn: {
    type: Boolean as PropType<boolean>,
    default: false,
  },
  /** 固定列数量*/
  lockColumnCount: {
    type: Number as PropType<number>,
    default: 0,
  },
  /** 操作列宽度设置*/
  operateColumnWidth: {
    type: Number as PropType<number>,
    default: 100,
  },
  /** 列的显示类型设置*/
  columnDistype: {
    type: Object as PropType<Record<string, any>>,
    default: {},
  },
  /** 自定义列宽，优先级低于用户自定义列宽*/
  columnWidth: {
    type: Object as PropType<Record<string, any>>,
    default: {},
  },
  /** 固定序号列*/
  fixedIndex: {
    type: String as PropType<string>,
    default: true,
  },
  /** 自定义列显示名称*/
  columnName: {
    type: Object as PropType<Record<string, any>>,
    default: {},
  },
  /** 列样式设置（标签列设置）*/
  columnStyle: {
    type: Array as PropType<
      {
        column: string;
        style: Record<string, any>;
      }[]
    >,
    default: [],
  },
  /** 支持排序的列*/
  sortColumns: {
    type: String as PropType<string>,
    default: "",
  },
  /** 行内可编辑字段*/
  editColumns: {
    type: String as PropType<string>,
    default: "",
  },
  /** 隐藏列*/
  hideColumns: {
    type: String as PropType<string>,
    default: "",
  },
  /** 是否开启分页*/
  enablePaging: {
    type: Boolean as PropType<boolean>,
    default: true,
  },
  /** 分页选择器*/
  pageCountList: {
    type: String as PropType<string>,
    default: "10,20,50,100",
  },
  /** 默认分页大小*/
  defaultPageSize: {
    type: Number as PropType<number>,
    default: 10,
  },
  /** 表格唯一名称，用于前端二次开发*/
  gridId: {
    type: String as PropType<string>,
    default: 0,
  },
  /** 表格渲染前后操作*/
  callback: {
    type: Object as PropType<renderCallback>,
    default: {},
  },
  /** 表格数据请求函数 */
  dataRequest: {
    // 获取列表数据函数API
    type: Function as PropType<Function>,
  },
  dataSource: {
    type: Array as PropType<Recordable[]>,
    default: null,
  },
  treeKey: {
    type: String as PropType<string>,
    default: "",
  },
  treeName: {
    type: String as PropType<string>,
    default: "",
  },
  btnsArr: {
    type: Array as PropType<Recordable[]>,
    default: null,
  },
  queryFormValue: {
    type: Object as PropType<Object>,
    default: {},
  },
  queryFormRef: {
    type: Object,
    default: () => null,
  },
  pageId: {
    type: String as PropType<String>,
    default: "",
  },
  query: {
    type: Object as PropType<Object>,
    default: {},
  },
  /**
   * 搜索
   */
  schema: {
    type: Object as PropType<SchemaProps>,
    default: null,
  },
  columns: {
    type: Array as PropType<TableColumn[]>,
    default: [],
  },
  /** 编辑行类型
   * @const `single`: 只能同时编辑一行
   * @const `multiple`: 同时编辑多行
   * @const `cell`: 可编辑单元格
   * @defaultValue `single`
   */
  editableType: {
    type: String as PropType<EditableType>,
    default: "single",
  },
  /** 单元格保存编辑回调 */
  onSave: {
    type: Function as PropType<OnSave>,
  },
  /** 单元格取消编辑回调 */
  onCancel: {
    type: Function as PropType<OnCancel>,
  },
  /** 只能编辑一行的的提示 */
  onlyOneLineEditorAlertMessage: String,
} as const;
export type DynamicTableProps = ExtractPropTypes<typeof dynamicTableProps> &
  TableProps;

export type DynamicTableInstance = InstanceType<typeof DynamicTable>;

export const dynamicTableEmits = {
  change: (...rest: OnChangeCallbackParams) => rest.length === 2,
  "update:columns": (val: any) => isBoolean(val),
  "toggle-advanced": (isAdvanced: boolean) => isBoolean(isAdvanced),
};

export type GetRowKey<RecordType> = (record: RecordType, index?: number) => Key;

export type DynamicTableEmits = typeof dynamicTableEmits;

export type DynamicTableEmitFn = EmitFn<DynamicTableEmits>;
